﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FU.SE0208.G1.Entities;

namespace FU.SE0208.G1.Controller
{
    public class StorageInHandler
    {
        public bool addnew(StorageIn obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("AddStorageIn", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@EmpID", SqlDbType.Int)).Value = obj.EmpID1;
                command.Parameters.Add(new SqlParameter("@InDate", SqlDbType.DateTime)).Value = obj.InDate1;
                command.Parameters.Add(new SqlParameter("@ProID", SqlDbType.Int)).Value = obj.ProID1;
                command.Parameters.Add(new SqlParameter("@Quantity", SqlDbType.Int)).Value = obj.Quantity1;
                command.Parameters.Add(new SqlParameter("@Price", SqlDbType.Float)).Value = obj.Price1;
                command.Parameters.Add(new SqlParameter("@CalUnit", SqlDbType.NVarChar)).Value = obj.CalUnit1;

                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return true;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool del(StorageIn obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("DeleteStorageIn", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@InID", SqlDbType.Int)).Value = obj.InID1;
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return true;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool edit(StorageIn obj, StorageIn newObj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("EditStorageIn", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@InID", SqlDbType.Int)).Value = obj.InID1;
                command.Parameters.Add(new SqlParameter("@EmpID", SqlDbType.Int)).Value = newObj.EmpID1;
                command.Parameters.Add(new SqlParameter("@InDate", SqlDbType.DateTime)).Value = newObj.InDate1;
                command.Parameters.Add(new SqlParameter("@ProID", SqlDbType.Int)).Value = newObj.ProID1;
                command.Parameters.Add(new SqlParameter("@Quantity", SqlDbType.Int)).Value = newObj.Quantity1;
                command.Parameters.Add(new SqlParameter("@Price", SqlDbType.Float)).Value = newObj.Price1;
                command.Parameters.Add(new SqlParameter("@CalUnit", SqlDbType.NVarChar)).Value = newObj.CalUnit1;

                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return true;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public DataSet getAll(string table)
        {
            DataSet ds = new DataSet("G1");
            DbHandler.getQuery(ds, "Select * from [" + table + "]", table);
            return ds;
        }
    }
}
